package com.dkm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Counselor;
import com.dkm.entity.Role;
import com.dkm.entity.SignInLog;
import com.dkm.entity.Student;
import com.dkm.service.CounselorService;
import com.dkm.service.StudentService;
import com.dkm.service.UsersService;
import com.dkm.util.LayuiJsonUilt;
import com.dkm.util.ResultUtil;

@RestController
@RequestMapping("/student")
public class StudentController {

	//注入学生业务层
	@Autowired
	private StudentService studentService;
	@Autowired
	private UsersService usersService;
	//注入咨询师业务层
	@Autowired
	private CounselorService counselorService;
	//多条件查询网络学生
	@GetMapping
	@RequiresPermissions(value = "student:query")
	public LayuiJsonUilt<Student> findAllStudents(String user_id,Student student,LayuiJsonUilt<Student> lj,HttpServletRequest request) {
		Map<String, Object> map=new HashMap<String, Object>();
		
		//根据id获取用户的角色集合
		List<Role> roles=usersService.getRolesByUser_id(user_id);
		if(roles!=null) {
			//遍历角色
			for (Role role : roles) {
				if("网络咨询师".equals(role.getRole_name())) {
					//根据用户id获取网络咨询师
					Counselor netCounselor=counselorService.getNetCounselorByUser_id(user_id);
					//加入网络咨询师id条件
					map.put("network_counselor_id", netCounselor.getCounselor_id());
					//退出循环
					break;
				}
				
				if("咨询师".equals(role.getRole_name())) {
					//根据用户id获取咨询师
					Counselor counselor=counselorService.getCounselorByUser_id(user_id);
					//加入咨询师id条件
					map.put("counselor_id", counselor.getCounselor_id());
					
					break;
				}
			}
		}
		
		//获取参数
		String createBeginDate=request.getParameter("createBeginDate");
		String createEndDate=request.getParameter("createEndDate");
		String visitBeginDate=request.getParameter("visitBeginDate");
		String visitEndDate=request.getParameter("visitEndDate");
		if(createBeginDate!=null&&createBeginDate!="") {
			map.put("createBeginDate", createBeginDate);
		}
		if(createEndDate!=null&&createEndDate!="") {
			map.put("createEndDate", createEndDate);
		}
		if(visitBeginDate!=null&&visitBeginDate!="") {
			map.put("visitBeginDate", visitBeginDate);
		}
		if(visitEndDate!=null&&visitEndDate!="") {
			map.put("visitEndDate", visitEndDate);
		}
		map.put("student",student);
		//创建pageable分页对象
		Pageable pageable=PageRequest.of(lj.getPage(), lj.getLimit());
		//获取jpa的page对象
		Page<Student> page=studentService.findAllStudents(map, pageable);
		//设置数据和数据长度
		lj.setData(page.getContent());
		lj.setCount(page.getTotalElements());
		return lj;
	}
	
	//添加学生
	@PostMapping
	@RequiresPermissions(value = "student:add")
	public ResultUtil addStudent(String user_id,Student student) {
		
		return studentService.addStudent(user_id, student);
	}
	
	//修改学生
	@PutMapping
	@RequiresPermissions(value = "student:update")
	public ResultUtil updateStudent(Student student) {
		
		return studentService.updateStudent(student);
	}
	
	//删除学生
	@DeleteMapping
	@RequiresPermissions(value = "student:delete")
	public ResultUtil deleteStudent(String sid) {
		
		return studentService.deleteStudent(sid);
	}
	
	//批量手动分配咨询师
	@GetMapping("/manualAllocation")
	@RequiresPermissions(value = "student:manualAllocation")
	public ResultUtil manualAllocation(String sids) {
		
		return counselorService.manualAllocation(sids);
	}
}
